<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserDs
 * 数据服务层
 * @author Administrator
 */
class UserDs {

    //put your code here

    static $_instance = null;

    public function __construct() {
       Yii::app()->cache->keyPrefix =User::model()->tableName();
    }

    public function getUserById($uid) {
        $value = Yii::app()->cache->get($uid);
       
        if ($value == false) {
       
            $value = User::model()->getUserById($uid);
            Yii::app()->cache->set($uid, $value);
        }
        return $value;
    }

    public function getUsersByIds($uids,$order='') {
        return User::model()->getUsersByIds($uids,$order);
    }

    public function getUsersByRand($num, $exceptUid = 0) {
        return User::model()->getUsersByRand($num, $exceptUid);
    }

    public function getUserByName($username) {
        return User::model()->getUserByUserNmae($username);
    }

    public function getUserByEmail($email) {
        return User::model()->getUserByEmail($email);
    }

    public function getUsersByGid($gid) {
        return User::model()->getUsersByGid($gid);
    }

    public function addUser(UserDm $dm) {
        if (true !== ($result = $dm->beforeAdd()))
            return $result;
        $uid = User::getInstance()->addUser($dm->getData());

        //  PwSimpleHook::getInstance('PwUser_add')->runDo($dm);
        return $uid;
    }

    /*
     * 编辑用户信息
     */

    public function editUser(UserDm $dm) {
        $r = $dm->beforeUpdate();
        if ($r instanceof BaseError) {
            return $r;
        } else {
            User::model()->editUser($dm->uid, $dm->getData());
            Yii::app()->cache->delete($dm->uid);
        }
    }

    /**
     * 更新用户积分
     *
     * @param object $dm
     * @return bool
     */
    public function updateCredit(UserDm $dm) {
        //  $result = User::model()->updateCredit($dm->getField('uid'), $dm->getField('coins'), $dm->getField('exp'));
        $uid = $dm->getField('uid');
        $credit = $dm->getData();
        unset($credit['uid']);
        $result = User::model()->updateCredit($uid, $credit);
        //  $result = User::model()-> editUser($dm->getField('uid'),$dm->getData());
        if (0 >= $result)
            return new BaseError(BaseError::E_PARAM);
        Yii::import('application.library.MtSimpleHook');
        //  MtSimpleHook::getInstance('credit_update')->runDo($dm->getField('uid'), $dm->getField('coins'), $dm->getField('exp'));
         Yii::app()->cache->delete($dm->getField('uid'));
        MtSimpleHook::getInstance('credit_update')->runDo($dm->getField('uid'), $dm->getData());
        return true;
    }

    public function getTeachList($gid, $start, $limit, $ext = array()) {
        // return User::model()->getUsersByGid($gid,$start,$limit,$ext);
        return MongoUserSession::getInstance()->getTeachList($gid, $start, $limit, $ext);
    }

    public function getTeachCount($gid, $ext = array()) {
        // return User::model()->coutUsersByGid($gid,$ext);
        return MongoUserSession::getInstance()->getTeachCount($gid, $ext);
    }

}
